package bb.lanxing.lib.devices.bryton.bbcp;

//import com.parse.codec.CharEncoding;
//import java.io.UnsupportedEncodingException;
import java.nio.charset.StandardCharsets;
import java.util.Date;

public class BbcpCommand implements BbcpPackage {
    private byte id;
    private byte[] payload;
    private byte seqNo;
    private byte type;

    public static BbcpCommand createWriteProfileCommand(byte b) {
        return new BbcpCommand((byte) 0, (byte) 3, b);
    }

    public static BbcpCommand createReadLogDataCommand(byte b, Date date) throws BbcpValidationException {
        return new BbcpCommand((byte) 0, (byte) 4, b, BbcpData.intToBytes((int) (date.getTime() / 1000)));
    }

    public static BbcpCommand createWriteAlarmSettingsCommand(byte b) {
        return new BbcpCommand((byte) 0, (byte) 5, b);
    }

    public static BbcpCommand createAckCommand(byte b, byte b2, byte b3) {
        return new BbcpCommand((byte) 2, b, b2, new byte[]{b3});
    }

    public static BbcpCommand createAckCommand(byte b, byte b2, short s) {
        byte[] bArr = {5};
        BbcpData.shortToBytes(s, bArr, 1);
        return new BbcpCommand((byte) 2, b, b2, bArr);
    }

    public static BbcpCommand createReadLogFileCommand(byte b, int i, byte b2) {
        byte[] bArr = new byte[5];
        System.arraycopy(BbcpData.intToBytes(i), 0, bArr, 0, 4);
        bArr[4] = b2;
        return new BbcpCommand((byte) 0, (byte) 12, b, bArr);
    }

    public BbcpCommand() {
        this.type = (byte) -1;
        this.id = (byte) -1;
    }

    public BbcpCommand(byte b, byte b2, byte b3) {
        this.type = b;
        this.id = b2;
        this.seqNo = b3;
        this.payload = null;
    }

    public BbcpCommand(byte b, byte b2, byte b3, byte[] bArr) {
        this.type = b;
        this.id = b2;
        this.seqNo = b3;
        this.payload = bArr;
    }

    @Override
    public byte[] pack() throws BbcpValidationException {
        byte[] bArr = this.payload;
        byte[] bArr2 = new byte[bArr != null ? bArr.length + 2 : 2];
        bArr2[0] = this.id;
        bArr2[1] = (byte) ((this.type << 5) | (this.seqNo << 2));
        if (bArr != null) {
            System.arraycopy(bArr, 0, bArr2, 2, bArr.length);
        }
        return bArr2;
    }

    @Override
    public void unpack(byte[] bArr) throws BbcpValidationException {
        if (bArr.length < 3) {
            throw new BbcpValidationException("invalid length of data");
        }
        this.id = bArr[0];
        this.type = (byte) ((bArr[1] & 224) >> 5);
        this.seqNo = (byte) ((bArr[1] & 28) >> 2);
        if (bArr.length <= 2) {
            return;
        }
        byte[] bArr2 = new byte[bArr.length - 2];
        this.payload = bArr2;
        System.arraycopy(bArr, 2, bArr2, 0, bArr2.length);
    }

    public byte getId() {
        return this.id;
    }

    public byte getSeqNo() {
        return this.seqNo;
    }

    public byte getType() {
        return this.type;
    }

    public byte[] getPayload() {
        return this.payload;
    }

    public byte getStatusCode() throws BbcpValidationException {
        byte b;
        byte[] bArr = this.payload;
        if (bArr.length < 1 || ((b = this.type) != 1 && b != 2)) {
            throw new BbcpValidationException("method only for response and action");
        }
        return bArr[0];
    }

    public static BbcpCommand createUserAddressCommand(byte b, String str) throws BbcpValidationException {
        return new BbcpCommand((byte) 0, (byte) 8, b, str.getBytes(StandardCharsets.US_ASCII/*CharEncoding.US_ASCII*/));
    }
}
